package shell

import (
    "bufio"
    "fmt"
    "io"
    "os/exec"
)

type LinuxShell struct {
    Name string
    Args [] string
}

func (shell LinuxShell) Exe() ShellResult {
    result := ShellResult{}
    cmd := exec.Command(shell.Name, shell.Args...)
    cmd.Stderr = &result.Stderr
    cmd.Stdout = &result.Stdout
    result.Error = cmd.Run()
    return result
}

func (shell LinuxShell) ExeRealTime() ShellResult {
    contentArray := make([]string, 0)
    result := ShellResult{}
    fmt.Println(shell.Args)
    cmd := exec.Command(shell.Name, shell.Args...)
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        fmt.Println(err.Error())
        result.Error = err
        return result
    }
    err = cmd.Start()
    if err != nil {
        fmt.Println(err.Error())
        result.Error = err
        return result
    }
    reader := bufio.NewReader(stdout)
    //实时循环读取输出流中的一行内容
    for {
        line, err2 := reader.ReadByte()
        if err2 != nil || io.EOF == err2 {
            break
        }
        fmt.Println(string(line))
        contentArray = append(contentArray, string(line))
    }
    err = cmd.Wait()
    if err != nil {
        fmt.Println(err.Error())
        result.Error = err
        return result
    }
    return result
}